package com.smit.livevideo.sample.dialog;

import android.app.Activity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.smit.dvb.CIMenuInfo;
import com.smit.livevideo.R;
import com.smit.livevideo.sample.controller.CIMMIController;
import com.smit.livevideo.sample.util.SDKAPIUtil;
import com.smit.livevideo.sample.view.CIListAdapter;
import com.smit.livevideo.sample.view.CIListItem;
import com.smit.utils.LogUtil;

import java.util.ArrayList;

/**
 * Created by smfang on 2015/12/28.
 * CI MMI Menu dialog
 */
public class CIMMIMenuDialog extends BaseDialogFragment implements DialogInterface.OnKeyListener, Handler.Callback {
    private static final String TAG = CIMMIMenuDialog.class.getSimpleName();
    private static final int MSG_CI_MMI_MENU_UPDATE = 1000;
    private TextView ciTitleText;
    private TextView ciSubTitleText;
    private TextView ciBottomText;
    private ListView ciItemListView;
    private CIListAdapter ciListAdapter;
    private ArrayList<CIListItem> ciArrayList;
    private CIMMIController cimmiController;
    private Handler msgHandler;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        LogUtil.trace(TAG, "onAttach~~~");
        this.activity = activity;
        msgHandler = new Handler(this);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onStart() {
        super.onStart();
        Window window = getDialog().getWindow();
        WindowManager.LayoutParams windowParams = window.getAttributes();
        //设置灰背景为透明
        windowParams.dimAmount = 0.00f;
        window.setAttributes(windowParams);
        //设置无边框
        window.getDecorView().setBackgroundColor(getResources().getColor(R.color.transparent_background));
        window.setLayout(activity.getResources().getDimensionPixelSize(R.dimen.px710),
                activity.getResources().getDimensionPixelSize(R.dimen.px460));
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        LogUtil.trace(TAG, "onCreateView~~~");
        View view = inflater.inflate(R.layout.dialog_ci_mmi_menu, container, false);
        ciItemListView = (ListView) view.findViewById(R.id.ci_item_list);
        ciTitleText = (TextView) view.findViewById(R.id.ci_title_text);
        ciSubTitleText = (TextView) view.findViewById(R.id.ci_sub_title);
        ciBottomText = (TextView) view.findViewById(R.id.ci_bottom_text);
        updateCIMMIMenu();
        initCIMMIMenuItem();
        getDialog().setOnKeyListener(this);
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        LogUtil.trace(TAG, "onResume~~~");
    }

    @Override
    public boolean handleMessage(Message msg) {
        updateCIMMIMenu();
        initCIMMIMenuItem();
        return false;
    }

    @Override
    public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
        LogUtil.trace(TAG, "onKey~~~");
        if (KeyEvent.KEYCODE_BACK == keyCode && event.getAction() == KeyEvent.ACTION_DOWN) {
            LogUtil.trace(TAG, "showCIView KEYCODE_BACK");
            SDKAPIUtil.getCIControl().selectMenuItem(0);
            return true;
        }

//        if(cimmiController.isServicesSwitch())
//        {
//            if ((KeyEvent.KEYCODE_DPAD_UP == keyCode || KeyEvent.KEYCODE_CHANNEL_UP == keyCode
//                    || KeyEvent.KEYCODE_MEDIA_REWIND == keyCode || 328 == keyCode)
//                    && event.getAction() == KeyEvent.ACTION_DOWN) {
//                LogUtil.trace(TAG, "showCIView KEYCODE_CHANNEL_UP");
//                CI_CONTROL.closeSession();
//                LivevideoUtil.playServicesRewind();
//                return true;
//            }
//
//            if ((KeyEvent.KEYCODE_DPAD_DOWN == keyCode || KeyEvent.KEYCODE_CHANNEL_DOWN == keyCode
//                    || KeyEvent.KEYCODE_MEDIA_FAST_FORWARD == keyCode || 331 == keyCode)
//                    && event.getAction() == KeyEvent.ACTION_DOWN) {
//                LogUtil.trace(TAG, "showCIView KEYCODE_CHANNEL_DOWN");
//                CI_CONTROL.closeSession();
//                LivevideoUtil.playServicesForward();
//                return true;
//            }
//        }
        return false;
    }

    /**
     * 发送消息刷新 CI MMI MENU
     */
    public void msgUpdateCIMMIMenu()
    {
        LogUtil.debug(TAG, "msgUpdateCIMMIMenu~~~");
        msgHandler.sendEmptyMessage(MSG_CI_MMI_MENU_UPDATE);
    }

    /**
     * 初始化CI MMI MENU listview
     */
    private void initCIMMIMenuItem() {
        LogUtil.debug(TAG, "initCIMMIMenuItem~~~");
        ciItemListView.setAdapter(ciListAdapter);
        ciItemListView.setFocusable(true);
        ciItemListView.setFocusableInTouchMode(true);
        ciItemListView.requestFocus();
        ciItemListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
                LogUtil.trace(TAG, "onItemClick in TVListView id=" + id);
                if (SDKAPIUtil.getCIControl() != null) {
                    LogUtil.debug(TAG, "initCIMMIMenuItem ciMenu is not null");
                    SDKAPIUtil.getCIControl().selectMenuItem((int) id + 1);
                }
            }
        });
    }

    /**
     * 刷新CI MMI MENU
     */
    private void updateCIMMIMenu() {
        LogUtil.debug(TAG, "updateCIMMIMenu~~~");
        if (SDKAPIUtil.getCIControl() == null)
        {
            LogUtil.debug(TAG, "CI_CONTROL is null!!!");
            return;
        }

        ciArrayList = new ArrayList<CIListItem>();
        int item = R.layout.dialog_ci_mmi_menu_item;
        ciListAdapter = new CIListAdapter(activity.getApplicationContext(), item, ciArrayList);
        CIMenuInfo ciMenuInfo = SDKAPIUtil.getCIControl().getCIMenuInfo();

        if (ciMenuInfo == null)
        {
            LogUtil.debug(TAG, "ciMenuInfo is null!!!");
            return;
        }

        if (ciMenuInfo != null) {
            if (ciTitleText != null) {
                ciTitleText.setText(ciMenuInfo.titleText);
            }
            if (ciSubTitleText != null) {
                ciSubTitleText.setText(ciMenuInfo.subTitleText);
            }
            if (ciBottomText != null) {
                ciBottomText.setText(ciMenuInfo.bottomText);
            }
            updateCIMMIMenuItems(ciMenuInfo.itemTextList);
        }
    }

    /**
     * 刷新CI MMI MENU listview
     * @param itemTextList
     */
    private void updateCIMMIMenuItems(ArrayList<String> itemTextList) {
        LogUtil.trace(TAG, "itemTextList.size():" + itemTextList.size());
        ciArrayList.clear();
        CIListItem item;
        synchronized (itemTextList) {
            for (String string : itemTextList) {
                item = new CIListItem();
                item.ciName = string;
                LogUtil.trace(TAG, "updateCIMMIMenuItems string:" + string);
                ciArrayList.add(item);
            }
        }
        ciListAdapter.notifyDataSetChanged();
    }
}
